﻿@{
    ViewBag.Title = "Vacation requests";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model WF.Sample.Models.DocumentListModel<DocumentModel>

<div class="wfe-application-toolbar">
    <a href="@Url.Action("Edit", "Document")" class="ui primary button">Create</a>
    <a onclick="DeleteSelected()" href="#" class="ui secondary button">Delete</a>
</div>
<table class="grid">
    <tbody>
    <tr class="grid-header">
        <th style="width:20px"></th>
        <th style="width:20px">Number</th>
        <th>State</th>
        <th>Name</th>
        <th>Comment </th>
        <th>Author</th>
        <th>Manager</th>
        <th>Sum</th>
    </tr>
    @foreach (var item in Model.Docs)
    {
        var editUrl = item.IsCorrect ? @Url.Action("Edit", "Document", new{Id = item.Id}) : "";
        <tr ondblclick="SetHref('@editUrl')">
            <td><div class="ui checkbox"><input type="checkbox" name="checkedbox" class="selectedValues" value="@item.Id" /><label></label></div> </td>
            <td><a href=@editUrl>@item.Number</a></td>
            <td>@item.StateName </td>
            <td><a href=@editUrl>@item.Name</a></td>
            <td>@item.Comment </td>
            <td>@item.AuthorName </td>
            <td>@item.ManagerName</td>
            <td>@item.Sum.ToString("n2")</td>
        </tr>
    }
</table>

@(await Html.PartialAsync("Partial/Paging"))

<script>

   function SetHref(url) {
       if (url)
        document.location.href = url;
    }
    function DeleteSelected() {
        var data = new Array();
        var selectedValues = $('.selectedValues:checked');

        if (selectedValues.length < 1) {
            alert('Please, select rows for deleting!');
            return;
        }

        for (var i = 0; i < selectedValues.length; i++) {
            data.push({ name: 'ids', value: selectedValues[i].value });
        }

        $.ajax({
            type: "POST",
            url:  "@Url.Action("DeleteRows", "Document")",
            data: data,
            success: function(msg) {
                alert(msg);
                location.reload();
            }
        });
    }
</script>